package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.RedisMessageConstant;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @Reference
    private MemberService memberService;


    /**
     * 详细步骤
     * 1.首先验证验证码是否正确，失败返回信息
     * @param map
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Map map) {
        try {
            //拿到客户输入的验证码
            String validateCode1 = (String) map.get("validateCode");
            //从redis中拿出验证码
            String telephone = (String) map.get("telephone");
            String validateCode2 = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
            if (validateCode2 == null || !validateCode1.equals(validateCode2)) {
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }

            //验证成功删除该号码的验证码
            jedisPool.getResource().del(telephone + SMSUtils.VALIDATE_CODE);

            //成功则继续往下开始预约
            //这里添加内部的异常为大异常，抛出的时候外面该如何处理？
            Order order = orderService.add(map);

            //预约成功发送短信
            //SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, "2333");

            return new Result(true, MessageConstant.ADD_ORDER_SUCCESS, order.getId());

        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ORDER_FAIL);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

    @RequestMapping("findById")
    public Result findById(@RequestParam("id") Integer id) {
        try {
            Map map = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    /**
     * 验证号码是否正确
     * @param loginInfo
     * @return
     */
    @RequestMapping("check")
    public Result check(@RequestBody Map loginInfo) {
        try {
            String telephone = (String) loginInfo.get("telephone");
            String validateCode2 = (String) loginInfo.get("validateCode");
            String key = telephone + RedisMessageConstant.SENDTYPE_LOGIN;
            String validateCode1 = jedisPool.getResource().get(key);
            if (validateCode1 == null || !validateCode1.equals(validateCode2)) {
               return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }

            //清空其redis中验证码数据
            jedisPool.getResource().del(key);

            //查询是否为会员，不是会员则添加为会员
            Member member = orderService.check(telephone);
            if (member == null) {
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                memberService.add(member);
            }

            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.LOGIN_ERROR);
        }

    }
}
